package com.jic.manager.controller.manage.conf;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.conf.api.manage.SensitiveInfoApi;
import com.jic.manager.entity.Sensitive;
import com.jic.conf.request.SensitiveRequest;
import com.jic.conf.response.SensitiveResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.SensitiveService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: zxl
 * @date: 2020-02-06
 * @remark: 评论敏感词库的服务
 */
@Api("评论敏感词库的服务")
@RestController
@RequestMapping("/sensitive")
@Slf4j
public class SensitiveController implements SensitiveInfoApi {

    @Autowired
   SensitiveService sensitiveService;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有敏感词")
    @ResponseBody
    @RequestMapping(value="/queryList")
    @Override
    public RestResult<PageResult<SensitiveResponse>> queryList(@RequestBody SensitiveRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        SensitiveRequest sensitiveRequest = BeanUtil.copy(form, SensitiveRequest.class);

        PageResult<Sensitive> result = sensitiveService.queryPage(sensitiveRequest,page);

        List<SensitiveResponse> sensitiveList = result.getRows().stream().map(temp -> {
            SensitiveResponse sensitiveResponse = BeanUtil.copy(temp, SensitiveResponse.class);
            return sensitiveResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<SensitiveResponse> pageResult = new PageResult<>();
        pageResult.setRows(sensitiveList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /**
     * 新增/编辑
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveSensitiveInfo")
    public RestResult saveSensitiveInfo(@RequestBody SensitiveRequest sensitiveRequest) {
        RestResult restResult=new RestResult();
        //Sensitive sensitive =BeanUtil.copy(sensitiveRequest,Sensitive.class);
        Long id = sensitiveRequest.getId();
        try {
            if (null != id && id !=0){
                long flag = sensitiveService.update(sensitiveRequest);
                restResult=RestResult.success(restResult);
            }else{
                long flag=sensitiveService.insert(sensitiveRequest);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }
}
